<template>
  <div class='CreateOrEditor'>
    <el-form ref="form" :model="form" label-width="80px">
      <el-form-item label="角色名称">
        <el-input v-model="form.name"></el-input>
      </el-form-item>
      <el-form-item label="角色编码">
        <el-input v-model="form.code"></el-input>
      </el-form-item>
      <el-form-item label="描述">
        <el-input type="textarea" v-model="form.description"></el-input>
      </el-form-item>
      <el-form-item class='foot'>
        <el-button type="primary" @click="onSubmit">确定</el-button>
        <el-button @click="$emit('cancel')">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script lang='ts'>
import Vue from 'vue'
import { createOrUpadte, getRoleById } from '@/services/role'
export default Vue.extend({
  name: 'CreateOrEditor',
  props: {
    isEditor: {
      type: [Boolean],
      default: false
    },
    roleId: {
      type: [Number, String]
    }
  },
  data () {
    return {
      form: {
        name: '',
        code: '',
        description: ''
      }
    }
  },
  created () {
    this.isEditorRole()
  },
  methods: {
    async isEditorRole () {
      if (this.isEditor === true) {
        const { data } = await getRoleById(this.roleId)
        this.form = data.data
      }
    },
    async onSubmit () {
      await createOrUpadte(this.form)
      this.$message.success('成功~')
      this.$emit('success')
    }
  }
})
</script>
<style lang='scss' scoped>
.foot {
  display: flex;
  justify-content: flex-end;
}
</style>
